package com.example.it.recyclerviewaddheaderandfooter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

/**
 * Created by 鲁迅认识的那只猹 on 9/5/2017 2:23 PM.
 * Emial 1258730808@qq.com
 * Desc:
 */

public class MyRecyclerViewAdapter extends BaseRecyclerAdapter<String> {
    /**
     * 构造函数
     *
     * @param context    上下文对象
     * @param dataSource 所需数据
     * @param resId      Item的布局资源Id
     */
    public MyRecyclerViewAdapter(Context context, List<String> dataSource, int resId) {
        super(context, dataSource, resId);
    }

    /**
     * 在此方法中，根据viewType参数判断需要如何实例化ViewHolder
     */
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == HEADER)
            return new MyViewHolder(getHeaderView());
        else if (viewType == FOOTER)
            return new MyViewHolder(getFooterView());
        else {
            View view = LayoutInflater.from(getContext()).inflate(R.layout.item, null, false);
            return new MyViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        //如果是页眉或者页脚，那么不进行任何处理
        if (isHeader(position))
            return;
        if (isFooter(position))
            return;

        int actualPosition = getActualPosition(position);
        String data = getDataSource().get(actualPosition);
        MyViewHolder myViewHolder = (MyViewHolder) holder;
        myViewHolder.tv.setText(data);
    }

    public class MyViewHolder extends BaseRecyclerAdapter.RecyclerViewHolder {

        private TextView tv;

        public MyViewHolder(View itemView) {
            super(itemView);
            tv = (TextView) itemView.findViewById(R.id.tv);
        }
    }
}
